package com.glsc.ngateway.platform.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.common.api.common.enums.PageConstant;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.SystemVendorInfo;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.api.platform.dto.system.ParamSystemVendorInfoDto;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.service.system.SystemVendorInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/system/vendor")
@Api(value = "信息系统供应商", tags = {"信息系统供应商"})
public class SystemVendorInfoController {

    private static final Logger logger = LoggerFactory.getLogger(SystemVendorInfoController.class);
    @Autowired
    private SystemVendorInfoService systemVendorInfoService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserCache userCache;
    @Autowired
    private UserService userService;

    /*
     * 新增或修改
     */
    @RequestMapping(method = RequestMethod.POST, value = "addOrUpdate")
    @ApiOperation(value = "新增或修改", notes = "新增或修改")
    public PlatformResponse<Void> addOrUpdateVirtual(@RequestBody SystemVendorInfo bean) {
        systemVendorInfoService.addOrUpdate(bean);
        return PlatformResponse.successMsg("创建或修改成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "delete")
    @ApiOperation(value = "删除", notes = "删除")
    public PlatformResponse<Void> delete(@RequestBody String param) throws Exception {
        if (StringUtils.isEmpty(param)) {
            return PlatformResponse.failedMsg("delete接口，id为空。");
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(param);
        String id = jsonObject.getString("id");
        if (StringUtils.isNotEmpty(id)) {
            systemVendorInfoService.delete(id);
        }
        return PlatformResponse.successMsg("删除成功");
    }

    /**
     * 查询
     * @param params
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "search")
    @ApiOperation(value = "查询", notes = "查询")
    public PlatformResponse<Page<SystemVendorInfo>> search(@RequestBody ParamSystemVendorInfoDto params) {
        if (params.getPageNo() == null) {
            params.setPageNo(PageConstant.PageNo);
        }
        if (params.getPageSize() == null) {
            params.setPageSize(PageConstant.PageSize);
        }
        Page<SystemVendorInfo> result = systemVendorInfoService.search(params);
        return PlatformResponse.successData(result);
    }

}